﻿/****************************************************
*	OSMF Edge Media Player version 1.2.0
* 
*	Author: Emanuele Manco
* 	E-Mail: hello@flashedge.net
*	Web: http://activeden.net/user/flashedge
*  
*****************************************************/

package net.flashedge.display 
{
	// flash libraries
	import flash.display.Sprite;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.filters.BitmapFilter;
	import flash.filters.DropShadowFilter;

	public class Tooltip extends Sprite
	{
		public var tooltip:Sprite = new Sprite();
		public var tooltipBg:Sprite = new Sprite();
		public var tooltipFilter:BitmapFilter;

		public var item:TextField = new TextField();
		public var itemFormat:TextFormat = new TextFormat();
		public var myFont:secondaryFont = new secondaryFont();

		public function Tooltip(cornerRadius:int, content:String, color:uint, txtColor:uint, alfa:Number, pointer:Boolean, dir:String, setup=null, custom:Boolean = false):void
		{
			var tooltip:Sprite = new Sprite();
			
			item = new TextField();
			item.autoSize = TextFieldAutoSize.LEFT;
			item.background = false;
			item.embedFonts = true;
			item.border = false;
			item.selectable = false;
			item.antiAliasType = AntiAliasType.NORMAL;
			item.y = -1;
				
			itemFormat.font = myFont.fontName;
			itemFormat.color = txtColor;
			if (setup == null) {
				itemFormat.size = 8;
			} else {
				itemFormat.size = setup.tooltipTextSize;	
			}
			//itemFormat.letterSpacing = -1;
			itemFormat.leftMargin = 3;
			itemFormat.rightMargin = 2;

			item.defaultTextFormat = itemFormat;
				
			item.htmlText = content;

			addChild(tooltip);
			tooltip.addChild(item);

			var radiusFactor = 0.1;
			
			tooltipBg = new Sprite();
	
			if (pointer) {
				switch (dir) {
					case "up":
						tooltipBg.graphics.lineStyle(1, 0xE2E2E2, 0.5, true);
						tooltipBg.graphics.beginFill(color, alfa);
						tooltipBg.graphics.moveTo(tooltip.width * .5, tooltip.height + 4.5);	// begin drawing the tooltip
						tooltipBg.graphics.lineTo(tooltip.width * .5 + 6, tooltip.height);
						tooltipBg.graphics.lineTo(tooltip.width - cornerRadius, tooltip.height);
						tooltipBg.graphics.curveTo(tooltip.width + (cornerRadius*radiusFactor), tooltip.height+(cornerRadius*radiusFactor), tooltip.width, tooltip.height-cornerRadius)
						tooltipBg.graphics.lineTo(tooltip.width, cornerRadius);
						tooltipBg.graphics.curveTo(tooltip.width + (cornerRadius * radiusFactor), -(cornerRadius * radiusFactor), tooltip.width - cornerRadius, 0);
						tooltipBg.graphics.lineTo(cornerRadius, 0);
						tooltipBg.graphics.curveTo(- (cornerRadius * radiusFactor), -(cornerRadius * radiusFactor), 0, cornerRadius);
						tooltipBg.graphics.lineTo(0, tooltip.height - cornerRadius);
						tooltipBg.graphics.curveTo(- (cornerRadius * radiusFactor), tooltip.height+(cornerRadius * radiusFactor), cornerRadius, tooltip.height);
						tooltipBg.graphics.lineTo(tooltip.width * .5 -6, tooltip.height);
					break;
					case "down":
						tooltipBg.graphics.lineStyle(1, 0xE2E2E2, 0.5, true);
						tooltipBg.graphics.beginFill(color, alfa);
						tooltipBg.graphics.moveTo(tooltip.width * .5, tooltip.height + 4.5);	// begin drawing the tooltip
						tooltipBg.graphics.lineTo(tooltip.width * .5 + 6, tooltip.height);
						tooltipBg.graphics.lineTo(tooltip.width - cornerRadius, tooltip.height);
						tooltipBg.graphics.curveTo(tooltip.width + (cornerRadius*radiusFactor), tooltip.height+(cornerRadius*radiusFactor), tooltip.width, tooltip.height-cornerRadius)
						tooltipBg.graphics.lineTo(tooltip.width, cornerRadius);
						tooltipBg.graphics.curveTo(tooltip.width + (cornerRadius * radiusFactor), -(cornerRadius * radiusFactor), tooltip.width - cornerRadius, 0);
						tooltipBg.graphics.lineTo(cornerRadius, 0);
						tooltipBg.graphics.curveTo(- (cornerRadius * radiusFactor), -(cornerRadius * radiusFactor), 0, cornerRadius);
						tooltipBg.graphics.lineTo(0, tooltip.height - cornerRadius);
						tooltipBg.graphics.curveTo(- (cornerRadius * radiusFactor), tooltip.height+(cornerRadius * radiusFactor), cornerRadius, tooltip.height);
						tooltipBg.graphics.lineTo(tooltip.width * .5 -6, tooltip.height);
						
						tooltipBg.scaleY *= -1;
						tooltipBg.y = tooltip.height;
						item.x = 1;
					
					break;
					case "left":
						tooltipBg.graphics.moveTo(tooltip.width, tooltip.height / 2 - 6);
						tooltipBg.graphics.lineTo(tooltip.width + 4.5, tooltip.height / 2 );
						tooltipBg.graphics.lineTo(tooltip.width, tooltip.height / 2 + 6);
					break;
					case "right":
						tooltipBg.graphics.moveTo(0, tooltip.height / 2 - 6);
						tooltipBg.graphics.lineTo(-4.5, tooltip.height / 2 );
						tooltipBg.graphics.lineTo(0, tooltip.height / 2 + 6);
					break;
					}
				}

			tooltipBg.graphics.endFill();
			tooltipBg.graphics.lineStyle();
			
			tooltip.addChild(tooltipBg);
			tooltip.swapChildren(tooltipBg, item);
			
			
			if (setup == null) {
				tooltipFilter = new DropShadowFilter(1, 90, 0x000000, 1, 2, 2, 2, 15);
				} else {
				tooltipFilter = new DropShadowFilter(1, 90, setup.tooltipGlowColor, setup.tooltipGlowAlpha, 2, 2, setup.tooltipGlowStrength, 15);
				}
				
			if (custom) { tooltipFilter = new DropShadowFilter(1, 90, 0x000000, 0.4, 4, 4, 2, 1);}
			tooltip.filters = [tooltipFilter];
		}
	}
}